package com.nuc.iblog.controler;

import com.nuc.iblog.entity.Music;
import com.nuc.iblog.entity.Picture;
import com.nuc.iblog.entity.User;
import com.nuc.iblog.entity.Video;
import com.nuc.iblog.jpa.cl.ClMusicJpa;
import com.nuc.iblog.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("cl")
public class MediaController {
    @Autowired
    MediaService mediaService;

    @RequestMapping("/getUserPicture")
    public String getUserPicture(HttpServletRequest request, @RequestParam(defaultValue = "0") int pagenum){
        User user = (User) request.getSession().getAttribute("User");
        Page<Picture> page=mediaService.getPagePicture(user,pagenum);
        request.setAttribute("Pictures",page.getContent());
        request.setAttribute("pagenums",page.getTotalPages());
        request.setAttribute("pagenum",pagenum);
        return "/cl/gallery";
    }

    @ResponseBody
    @RequestMapping("/getUserVideo")
    public List<Video> getUserVideo(HttpServletRequest request,@RequestParam(defaultValue = "0")int pagenum){
        User user=(User)request.getSession().getAttribute("User");
        Page<Video> page=mediaService.getPageVideo(user,pagenum);
        request.setAttribute("Videos",page.getContent());
        request.setAttribute("pagenums",page.getTotalPages());
        request.setAttribute("pagenum",pagenum);
        return page.getContent();
    }


    @RequestMapping("/toUploadMusic")
    public String toupLoadMusic(HttpServletRequest request){
        HttpSession session=request.getSession();
        request.setAttribute("User",(User)session.getAttribute("User"));
        return "/cl/upLoadMusic";
    }
    @RequestMapping("/getUserMusic")
    public String  getUserMusic(HttpServletRequest request,@RequestParam(defaultValue = "0")int pagenum){
        User user=(User)request.getSession().getAttribute("User");
        Page<Music> page=mediaService.getPageMusic(user,pagenum);
        request.setAttribute("Musics",page.getContent());
        request.setAttribute("pagenums",page.getTotalPages());
        request.setAttribute("pagenum",page);
        return "/cl/mymusic";

    }
    @ResponseBody
    @RequestMapping("/getPicture")
    public Picture getPicture(@RequestParam("pid")int pid){
        return mediaService.getPicture(pid);
    }
    @ResponseBody
    @RequestMapping("/getVideo")
    public Video getVideo(@RequestParam("vid")int vid){
        return mediaService.getVideo(vid);
    }

    @RequestMapping("/getMusic")
    public String getMusic(@RequestParam("mid")int mid,HttpServletRequest request){
        request.setAttribute("music",mediaService.getMusic(mid));
        return "/cl/music";
    }

    @ResponseBody
    @RequestMapping("/deletePicture")
    public Map<String,String> deletePicture(@RequestParam("pid")int pid){
        Map<String,String> returnMap=new HashMap<String, String>();
        if(mediaService.deletePicture(pid)==1){
            returnMap.put("msg","删除成功");
            returnMap.put("code","1");
            return returnMap;
        }else {
            returnMap.put("msg","删除失败");
            returnMap.put("code","0");
            return returnMap;
        }
    }
    @ResponseBody
    @RequestMapping("/deleteMusic")
    public Map<String,String> deleteMusic(@RequestParam("mid")int mid){
        Map<String,String> returnMap=new HashMap<String, String>();
        if(mediaService.deleteMusic(mid)==1){
            returnMap.put("msg","删除成功");
            returnMap.put("code","1");
            return returnMap;
        }else {
            returnMap.put("msg","删除失败");
            returnMap.put("code","0");
            return returnMap;
        }
    }
    @ResponseBody
    @RequestMapping("/deleteVideo")
    public Map<String,String> deleteVideo(@RequestParam("vid")int vid){
        Map<String,String> returnMap=new HashMap<String, String>();
        if(mediaService.deleteVideo(vid)==1){
            returnMap.put("msg","删除成功");
            returnMap.put("code","1");
            return returnMap;
        }else {
            returnMap.put("msg","删除失败");
            returnMap.put("code","0");
            return returnMap;
        }
    }


}
